<?php
/**
 * Created by PhpStorm.
 * User: wangzb
 * Date: 2016/4/23
 * Time: 16:33
 */
namespace Admin\Controller;
use Think\Page;

class NewsController extends BaseController
{
    public function indexAction()
    {
        $News = D('News');
        $condition = array();
        $count = $News->where($condition)->count();
        $Page = new Page($count, 50);
        $show = $Page->show();
        $list = $News->where($condition)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach($list as $key => $item) {
            $list[$key]['cat_info'] = D('NewsCategory')->getCatInfo($item['cat_id']);
        }
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->display();
    }

    public function addAction()
    {
        if (IS_POST) {
            $News = D('News');
            $_POST['News']['is_rec'] = isset($_POST['News']['is_rec']) ? (int)$_POST['News']['is_rec'] : 0;
            $data = $News->create($_POST['News']);
            if (false !== $data) {
                $insertId = $News->add();
                if (false !== $insertId) {
                    $this->success('新增成功', session('referr_url'));
                } else {
                    $this->error('更新失败（失败原因：' . $News->getError() . '）');
                }
            } else {
                $this->error($News->getError());
            }
        } else {
            $cat_list = D('NewsCategory')->getCatList();
            $this->assign('cat_list', $cat_list);
            $this->display();
        }
    }

    public function updateAction()
    {
        $News = M('News');
        if (IS_POST) {
            $_POST['News']['is_rec'] = isset($_POST['News']['is_rec']) ? (int)$_POST['News']['is_rec'] : 0;
            $data = $News->create($_POST['News']);
            if (false !== $data) {
                if (false !== $News->save()) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败（失败原因：' . $News->getError() . '）');
                }
            } else {
                $this->error($News->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $news_info = $News->find($id);
            if (false === $news_info) {
                $this->error('获取信息失败!');
            }
            $this->assign('news_info', $news_info);
            $cat_list = D('NewsCategory')->getCatList();
            $this->assign('cat_list', $cat_list);
            $this->display();
        }
    }
    
    public function deleteAction()
    {
        $id = I('get.id', 0, 'intval');
        $News = M('News');
        if (false !== $News->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * 编辑器上传处理
     */
    public function uploadJsonAction()
    {
        $config = array(
            'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'news/',
            'subPath' => 'news/',
        );
        vendor('Kindeditor.UploadJson', '', '.class.php');
        $UploadJson = new \UploadJson($config);
        $UploadJson->upload();
    }

    /**
     * 编辑器上传文件管理
     */
    public function fileManagerJson()
    {

    }
}